package life.majiang.community.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.HashMap;
import java.util.Map;

public class TokenUtil {

    private static final String sign = "@i!t&a#n%y";

    public static String getToken(Integer id,String name,String Ttoken,Integer languageid){
        String token = JWT.create()
                          .withClaim("id",id)
                          .withClaim("name",name)
                          .withClaim("Ttoken",Ttoken)
                          .withClaim("languageid",languageid!=null?languageid:-1)
                          .sign(Algorithm.HMAC256(sign));
        return token;
    }

    public static Map<String,Object> analysisToken(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(sign)).build();
        DecodedJWT  verifier =  jwtVerifier.verify(token);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",verifier.getClaim("id").asInt());
        map.put("name",verifier.getClaim("name").asString());
        map.put("Ttoken",verifier.getClaim("Ttoken").asString());
        if(verifier.getClaim("languageid")!=null){
            map.put("languageid",verifier.getClaim("languageid").asInt());
        }
        return map;
    }

}
